package pdd;

import java.util.*;

/**
 * @author zhangmin
 * @create 2022-03-20 19:10
 */
public class PDD_0320_2 {
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        int t=cin.nextInt();
        for (int k = 0; k < t; k++) {
            int n=cin.nextInt();//瓜田数量
            int v=cin.nextInt();//每天最多
            int[][] ab=new int[n][3];
            int maxDay=0;
            for (int i = 0; i < n; i++) {
                ab[i][0]=cin.nextInt();
                ab[i][1]=cin.nextInt();
                ab[i][2]=cin.nextInt();
                maxDay=Math.max(maxDay,ab[i][2]);
            }
            int res=0;
            Arrays.sort(ab, new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    return o1[2]==o2[2]?o1[1]-o2[1]:o1[2]-o2[2];
                }
            });

            for (int i = 1; i <= maxDay; i++) {
                int max=v;
                for (int j = 0; j < n; j++) {
                    if (i>=ab[j][1]&&i<=ab[j][2]){
                        if (max==0) break;
                        if(max<=ab[j][0]){
                            res+=max;
                            ab[j][0]-=max;
                            break;
                        }else {
                            res+=ab[j][0];
                            max-=ab[j][0];
                            ab[j][0]=0;
                        }
                    }
                }
            }
            System.out.println(res);
        }
    }
}
